const Controller = require("egg").Controller;
const XLSX = require("xlsx");

class CounterController extends Controller {
  async test() {
    const { ctx } = this;
    await this.app.redis.set('user', 'lijie')
    const res = await this.app.redis.get('user')
    console.log('res', res);

    // // 读取Excel文件
    // var workbook = XLSX.readFile("C:\Users\86182\Desktop\study\backend\counter\app\controller\xindu.XLSX");
    // var worksheet = workbook.Sheets[workbook.SheetNames[0]];

    // // 将工作表转换为JSON对象
    // var data = XLSX.utils.sheet_to_json(worksheet);

    console.log("data");
    ctx.body = {
      data: '123'
    };
  }

  // 添加计数器
  async addCounter() {
    const { ctx } = this;
    const {
      target,
      target_amount,
      current_amount = 0,
      add_step = 1,
      decrease_step = 1,
      start_time = "",
      end_time = "",
    } = ctx.request.body;
    const result = await ctx.service.counter.addCounter(
      target,
      target_amount,
      current_amount,
      add_step,
      decrease_step,
      start_time,
      end_time
    );
    if (result) {
      ctx.body = {
        status: 200,
        message: "新增成功",
        data: {},
      };
    } else {
      ctx.body = {
        status: 201,
        message: "新增失败",
        data: {},
      };
    }
  }

  // 获取计数器详情
  async getCounterDetail() {
    const { ctx } = this;
    const { id } = ctx.request.query;
    const result = await ctx.service.counter.getCounterDetail(id);
    if (result) {
      ctx.body = {
        status: 200,
        message: "查询成功",
        data: result,
      };
    } else {
      ctx.body = {
        status: 201,
        message: "查询失败",
        data: {},
      };
    }
  }

  // 删除学生信息
  async delStu() {
    const { ctx } = this;
    ctx.body = {
      status: 200,
      message: "删除成功",
      data: {},
    };
  }

  // 更新当前数量
  async updateCurrentAmount() {
    const { ctx } = this;
    const { id, type } = ctx.request.body;
    const result = await ctx.service.counter.getCounterDetail(id);
    let current_amount = result["current_amount"];

    if (type === "add_step") {
      current_amount += result["add_step"];
    }
    if (type === "decrease_step") {
      current_amount -= result["decrease_step"];
    }
    if (current_amount < 1) {
      current_amount = 1;
    }
    const data = await ctx.service.counter.updateCurrentAmount(
      id,
      current_amount
    );
    if (data) {
      ctx.body = {
        status: 200,
        message: "更新成功",
        data: {},
      };
    } else {
      ctx.body = {
        status: 201,
        message: "更新失败",
        data: {},
      };
    }
  }

  // 更新一个计数器信息
  async updateOneCounter() {
    const { ctx } = this;
    const {
      id,
      target,
      target_amount,
      current_amount = 0,
      add_step = 1,
      decrease_step = 1,
      start_time,
      end_time,
    } = ctx.request.body;
    const result = await ctx.service.counter.updateOneCounter(
      id,
      target,
      target_amount,
      current_amount,
      add_step,
      decrease_step,
      start_time,
      end_time
    );
    if (result) {
      ctx.body = {
        status: 200,
        message: "修改成功",
        data: {},
      };
    } else {
      ctx.body = {
        status: 201,
        message: "修改失败",
        data: {},
      };
    }
  }
  // 删除一个计数器信息
  async deleteOneCounter() {
    const { ctx } = this;
    const { id } = ctx.request.query;
    const result = await ctx.service.counter.deleteOneCounter(id);
    if (result) {
      ctx.body = {
        status: 200,
        message: "删除成功",
        data: {},
      };
    } else {
      ctx.body = {
        status: 201,
        message: "删除失败",
        data: {},
      };
    }
  }

  // 重置一个计数器信息
  async resetOneCounter() {
    const { ctx } = this;
    const { id } = ctx.request.query;
    const result = await ctx.service.counter.resetOneCounter(id);
    if (result) {
      ctx.body = {
        status: 200,
        message: "重置成功",
        data: {},
      };
    } else {
      ctx.body = {
        status: 201,
        message: "重置失败",
        data: {},
      };
    }
  }
  // 查询所有计数信息
  async getCounter() {
    const { ctx } = this;
    const result = await ctx.service.counter.getCounter();

    if (result) {
      ctx.body = {
        status: 200,
        message: "查询成功",
        data: result,
      };
    } else {
      ctx.body = {
        status: 201,
        message: "查询失败",
        data: {},
      };
    }
  }
}
module.exports = CounterController;
